package com.tiancheng.onecode.controller;

import com.tiancheng.onecode.model.request.MessageBody;
import com.tiancheng.onecode.model.response.Result;

import com.tiancheng.onecode.service.NotifyService;
import com.tiancheng.onecode.trade.model.request.TradeMerchantStatusReq;
import com.tiancheng.onecode.utils.JsonUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: kellen
 * @Description:
 * @Date: create in 2024/12/4 11:11
 */
@Slf4j
@RequestMapping("/notify")
@RestController
public class NotifyController {

    @Resource
    private NotifyService notifyService;

    @RequestMapping("/payCallback")
    public Result<String> payCallBack(@RequestBody MessageBody messageBody) {
        log.info("接受到支付中心支付消息回调：{}", JsonUtil.toJsonString(messageBody));
        notifyService.orderNotify(messageBody);
        return Result.success("消息回调处理成功！");
    }

    @RequestMapping("/merchantCallback")
    public Result merchantCallback(@RequestBody TradeMerchantStatusReq tradeMerchantStatusReq) {
        log.info("接受到支付中心商户进件消息回调：{}", JsonUtil.toJsonString(tradeMerchantStatusReq));
        try {
            notifyService.merchantNotify(tradeMerchantStatusReq);
        } catch (Exception e) {
            log.error("商户进件消息处理异常:", e);
            return Result.fail("商户进件消息处理异常");
        }
        return Result.success("消息回调处理成功！");
    }

}
